<?php
namespace Wx\Controller;
use Think\Controller;
/*
*乡村
*/
class VillageController extends CommonController {
	public function distance(){
		$star_ln=$_GET['star_ln'];
		$star_la=$_GET['star_la'];
		$end_ln=$_GET['end_ln'];
		$end_la=$_GET['end_la'];
		$alt=$this->getDistance($star_ln,$star_la,$end_ln,$end_la);
		$this->ajaxReturn($alt["distance"]);
	}//end fun
	/** 
	 * @param int $lat1 纬度1 
	 * @param int $lng1 经度1 
	 * @param int $lat2 纬度2 
	 * @param int $lng2 经度2 
	 * @return array 
	 */  
	function getDistance($lng1=0,$lat1=0,$lng2=0,$lat2=0)  
	{  
		$ak = '5nu3kfKee1AbTKHRoEIPDhI6hlDPWVSA';//您的百度地图ak，可以去百度开发者中心去免费申请    
		$distance = array();  
		$distance['distance'] = 0.00;//距离 公里  
		$distance['duration'] = 0.00;//时间 分钟  
		$url = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins='.$lat1.','.$lng1.'&destinations='.$lat2.','.$lng2.'&ak='.$ak;  
		$data = file_get_contents($url); 
		$data = json_decode($data,true);  
		if (!empty($data) && $data['status'] == 0) {  
			$distance['distance'] =preg_replace('/[^\.0123456789]/s', '', $data['result'][0]['distance']['text']); //计算距离  
		}  
		//{"status":0,"result":[{"distance":{"text":"118.3公里","value":118333},"duration":{"text":"3小时","value":10679}}],"message":"成功"}
		return $distance;  
	}  
	//乡村首页 
    public function country(){
		//$distance=$this->getDistance(114.613662,30.724923,113.613884,30.468778);dump($distance);
		$village=D('village');
		$all=array();
		$page=1;
		if($_GET['page'])$page=$_GET['page']; 
		if($_GET['keyword']){
			$all=$village->where("title like '%".$_GET['keyword']."%'")->order('supports desc')->page($page.',3')->select(); // 查询第一页数据
			$this->assign('keyword',$_GET['keyword']);
			}else{
				$all=$village->page($page.',3')->order('supports desc')->select(); // 查询第一页数据
			}
		$area=D('area');
		$village_admin=D('village_admin');
		$user_info=D('user_info');
		foreach($all as $i=>$one){
			//去掉标签且留90字符
		$all[$i]['content']=str_replace(array("\r\n", "\r", "\n","　"), "", $all[$i]['content']);
		$all[$i]['content']=preg_replace(array("(<[^>]*>)" ),"",$all[$i]['content']);//去掉所有标签
		$all[$i]['content']=str_replace(' ', '', $all[$i]['content']);//去掉空格
			$all[$i]['content']=mb_substr($all[$i]['content'],0,90,'utf-8');
			$img=explode(',',$one['imgs']);
			$all[$i]['f_img']=$img[0];//第一张图片作为封面图
			
			$getarea=$area->where("id=".$one['area_lev_1'])->find();
			$all[$i]['a1']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_2'])->find();
			$all[$i]['a2']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_3'])->find();
			$all[$i]['a3']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_4'])->find();
			$all[$i]['a4']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_5'])->find();
			$all[$i]['a5']=$getarea['name'];
			//书记信息
			$getva=$village_admin->where("v_id=".$one['id'])->find();
			if($getva['user_id']){
				$getinfo=$user_info->where("u_id=".$getva['user_id'])->find();
				$all[$i]['name']=$getinfo['name'];
				$all[$i]['va_img']=$getinfo['img'];
				$all[$i]['supports']=$getinfo['supports'];
			}
		}//end for
		if($_GET['page'])$this->ajaxReturn($all,'JSON');
		$this->assign('all',$all);//dump($all);
        $this->display('country'); // 输出模板
    }//end fun
	
	public function get_ln_la($address=''){
				//上传地址，获得经度纬度
				$newdata = urlencode($address);
				$url = "http://api.map.baidu.com/geocoder/v2/?address=".$newdata."&output=json&ak=5nu3kfKee1AbTKHRoEIPDhI6hlDPWVSA";
				$address_data = file_get_contents($url);
				$json_data = json_decode($address_data);//dump($json_data);
				$data=array();
				$data['lng'] = $json_data->result->location->lng;
				$data['lat'] = $json_data->result->location->lat;
				return $data;
	}//end fun
	//乡村详情
    public function info(){
		if(is_numeric($_GET['id'])){
			//乡村信息
			$village=D('village');
			$getinfo=$village->where("id=".$_GET['id'])->find();
			$area=D('area');
			$getarea=$area->where("id=".$getinfo['area_lev_1'])->find();
			$getinfo['a1']=$getarea['name'];
			$getarea=$area->where("id=".$getinfo['area_lev_2'])->find();
			$getinfo['a2']=$getarea['name'];
			$getarea=$area->where("id=".$getinfo['area_lev_3'])->find();
			$getinfo['a3']=$getarea['name'];
			$getarea=$area->where("id=".$getinfo['area_lev_4'])->find();
			$getinfo['a4']=$getarea['name'];
			$getarea=$area->where("id=".$getinfo['area_lev_5'])->find();
			$getinfo['a5']=$getarea['name'];
			$getinfo['img_arr']=explode(',',$getinfo['imgs']);
			if($getinfo['longitude']=='0'){
				//获取经纬度
				$re=$this->get_ln_la($getinfo['a1'].$getinfo['a2'].$getinfo['a3'].$getinfo['a4'].$getinfo['a5']);
				//更新经纬度到数据库
				$ll['longitude']=$re['lng'];
				$ll['latitude']=$re['lat'];
				$village->where("id=".$_GET['id'])->save($ll);
				$getinfo['longitude']=$ll['longitude'];
				$getinfo['latitude']=$ll['latitude'];
				
			}
			//计算距离 
			$ipaddress = get_client_ip();//dump($ipaddress);
			$Ip = new \Org\Net\IpLocation('UTFWry.dat'); // 实例化类
			$area =$location = $Ip->getlocation($ipaddress); // 获取某个IP地址所在的位置
			//dump($area["country"]);
			$star=$this->get_ln_la($area["country"]);//dump($star);
			$alt=$this->getDistance($star['lng'],$star['lat'],$getinfo['longitude'],$getinfo['latitude']);
			$getinfo['distance']=$alt["distance"];
			
			$this->assign('info',$getinfo);//dump($getinfo);
			
			//第一书记
			$village_admin=D('village_admin');
			$getvadmin=$village_admin->where("v_id=".$_GET['id'])->find();
			$user_info=D('user_info');
			$getva=$user_info->where("u_id=".$getvadmin['user_id'])->find();
			$getvadmin['name']=$getva['name'];
			$getvadmin['img']=$getva['img'];
			$getvadmin['info']=$getva['info'];
			$getvadmin['supports']=$getva['supports'];
			//日志数量
			$arc_diary=D('arc_diary');
			$getvadmin['d_num']=$arc_diary->where("u_id=".$getvadmin['user_id'])->count();
			$this->assign('vainfo',$getvadmin);//dump($getvadmin);
			//乡村贤达
			$arc_celebrity=D('arc_celebrity');
			$getce=$arc_celebrity->where("v_id=".$_GET['id'])->limit(0,4)->select();
			$this->assign('ceinfo',$getce);//dump($getce);
			//乡村特产
			$arc_farming=D('arc_farming');
			$getf=$arc_farming->where("v_id=".$_GET['id'])->limit(0,2)->select();
			//内容提取首页图片
			foreach($getf as $i=>$one){
				$imgarr=array();
				preg_match_all('/<img.*?src="(.*?)".*?>/is',$one['content'],$imgarr);
				$getf[$i]['f_img']=$imgarr[1][0];
				}//end for
			$this->assign('finfo',$getf);//dump($getf);
			//乡村夜话
			$arc_stories=D('arc_stories');
			$gets=$arc_stories->where("v_id=".$_GET['id'])->limit(0,2)->select();
			//内容提取首页图片
			foreach($gets as $i=>$one){
				$imgarr=array();
				preg_match_all('/<img.*?src="(.*?)".*?>/is',$one['content'],$imgarr);
				$gets[$i]['f_img']=$imgarr[1][0];
				}//end for
			$this->assign('sinfo',$gets);//dump($gets);

			$this->display('countryDetail'); // 输出模板
			}else{
			}//
        
    }//end fun
	
	//乡村点赞
	public function village_supports(){
		$village=D('village');
		if(is_numeric($_GET['id'])){
			if($_GET['dowhat']=='add'){
				$village->where("id=".$_GET['id'])->setInc('supports'); // 阅读加1
				}else{
					$village->where("id=".$_GET['id'])->setDec('supports'); // 阅读减1
				}
		}
	}//end fun
	
	//查找乡村
    public function country_ls(){
		$village=D('village');
		$all=array();
		$page=1;
		if($_GET['page'])$page=$_GET['page']; 
		if($_GET['keyword']){
			$all=$village->where("title like '%".$_GET['keyword']."%'")->order('supports desc')->page($page.',3')->select(); // 查询第一页数据
			$this->assign('keyword',$_GET['keyword']);
			}else{
				$all=$village->page($page.',3')->order('supports desc')->select(); // 查询第一页数据
			}
		foreach($all as $i=>$one){
			$img=explode(',',$one['imgs']);
			$all[$i]['f_img']=$img[0];//第一张图片作为封面图
			$area=D('area');
			$getarea=$area->where("id=".$one['area_lev_1'])->find();
			$all[$i]['a1']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_2'])->find();
			$all[$i]['a2']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_3'])->find();
			$all[$i]['a3']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_4'])->find();
			$all[$i]['a4']=$getarea['name'];
			$getarea=$area->where("id=".$one['area_lev_5'])->find();
			$all[$i]['a5']=$getarea['name'];
			//查询书记 图片 姓名 点赞
			$village_admin=D('village_admin');
			$getva=$village_admin->where("v_id=".$one['id'])->find();
			$user_info=D('user_info');
			$getinfo=$user_info->where("u_id=".$getva['user_id'])->find();
			$all[$i]['u_name']=$getinfo['name'];
			$all[$i]['u_img']=$getinfo['img'];
			$all[$i]['u_supports']=$getinfo['supports'];
			}//end for
		if($_GET['page'])$this->ajaxReturn($all,'JSON');
		$this->assign('all',$all);//dump($all);
		//dump($_GET);
        $this->display('search-xc'); // 输出模板
    }//end fun
	
}